package com.senior.sendmessage.config;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.senior.sendmessage.entity.CityListDTO;
import com.senior.sendmessage.entity.MessageLogPO;
import com.senior.sendmessage.entity.WeatherResultDTO;
import com.senior.sendmessage.service.MessageLogService;
import com.senior.sendmessage.service.WeatherService;
import com.senior.sendmessage.xxl.WeatherScheduleJob;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author Baijl
 * @version 0.0.1
 * @date 2021-11-21 10:48
 * @description TODO
 */
@Component
@RequiredArgsConstructor
public class WxChatMessageHandler implements WxMpMessageHandler {
    private final MessageLogService messageLogService;
    private final WeatherService weatherService;
    private final WeatherScheduleJob weatherScheduleJob;


    @Override
    public WxMpXmlOutMessage handle(final WxMpXmlMessage wxMessage, final Map<String, Object> context, final WxMpService wxMpService, final WxSessionManager sessionManager) {
        final CityListDTO cityListDTO = weatherScheduleJob.getCityList();
        final List<CityListDTO.ResultDTO> dtos = cityListDTO.getResult();
        if (CollUtil.isEmpty(dtos)) {
            return WxMpXmlOutMessage.TEXT().content("查询有误，请稍后再试！").fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
        }
        final StringBuilder builder = StrUtil.builder();
        for (final CityListDTO.ResultDTO x : dtos) {
            if (StrUtil.equals(x.getDistrict(), wxMessage.getContent())) {
                final WeatherResultDTO weatherResultDTO = weatherService.simpleWeatherByCityName(x.getId());
                if (0 != weatherResultDTO.getErrorCode()) {
                    final MessageLogPO messageLogPO = messageLogService.getMessageLog(LocalDateTimeUtil.formatNormal(LocalDate.now()));
                    return WxMpXmlOutMessage.TEXT().content("现在是" + LocalDateTimeUtil.formatNormal(LocalDateTimeUtil.now()) + " " + messageLogPO.getWeek()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
                }
                final WeatherResultDTO.ResultDTO result = weatherResultDTO.getResult();
                builder.append(result.getCity()).append("\n")
                        .append("\n")
                        .append("现在天气：")
                        .append(result.getRealtime().getInfo())
                        .append("\n")
                        .append("现在温度：")
                        .append(result.getRealtime().getTemperature())
                        .append("\n")
                        .append("现在湿度：")
                        .append(result.getRealtime().getHumidity())
                        .append("\n")
                        .append("风向：")
                        .append(result.getRealtime().getDirect())
                        .append("\n")
                        .append("风力：")
                        .append(result.getRealtime().getPower())
                        .append("\n")
                        .append("空气质量：")
                        .append(result.getRealtime().getAqi());

                return WxMpXmlOutMessage.TEXT().content(builder.toString()).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
            }
        }
        return WxMpXmlOutMessage.TEXT().content("找不到该城市，请重新输入！\n（城市名可以具体到区县，该功能目前支持查询实时天气！）").fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
    }
}
